package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Service
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result<PageResult> page(SetmealPageQueryDTO dto) {
        //非空判断
        if (dto.getPage()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if (dto.getPageSize()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //开启分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //调用mapper
        Page<SetmealVO> page = setmealMapper.getPage(dto);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
//        SetmealVO setmealVO=new SetmealVO();
//        String categoryName = setmealVO.getCategoryName();
        //封装
        PageResult pageResult= new PageResult(total,result);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     */
    @Override
    public Result<SetmealVO> getbyId(Long id) {
        if (id==null){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //调用mapper
        SetmealVO setmealVO = setmealMapper.getbyId(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result updateSetmeal(SetmealDTO dto) {
        //
        if (dto.getId()==null){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        //调用mapper
        setmealMapper.updateSetmeal(setmeal);
        return Result.success();
    }

    /**
     * 新增套餐
     * @param dto
     * @return
     */
    @PostMapping
    public Result insetSetmeal(SetmealDTO dto){
        if (dto.getId()==null){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (dto.getName()==null){
            return Result.error(MessageConstant.ACCOUNT_EXISTS_FOUND);
        }
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.insetSetmeal(setmeal);
        //获取生成的套餐id
        Long setmealid = setmeal.getId();
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealid));
        return Result.success();
    }
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public Result deleteSetmeal(List<Long> ids) {
        ids.forEach(id-> {
            //获取菜品id
            Setmeal dishById = setmealMapper.getDishById(id);
            //如果启售状态为1 ，不能被删除
            if (StatusConstant.ENABLE==dishById.getStatus()){
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        ids.forEach(id->{
        setmealMapper.deleteSetmeal(id);
        });
        return Result.success();
    }
    /**
     * user
     */
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
